﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Kevin.Shop.Business.Core.Components.Responser;
using Kevin.Shop.Business.SalesManagement.Components;
using Kevin.Shop.Business.Service.Entity.Helper;
using RiderShop.Mobile.Modle.MyAccount;
using RiderShop.Mobile.ServiceEntity.MyAccount;
using Kevin.Shop.Business.Core.Components;
using Kevin.Shop.Business.SalesManagement.Components.Order;
using RiderShop.Mobile.Common.Component;
using Kevin.Shop.Business.SalesManagement.Enumerations;

namespace RiderShop.Mobile.Controllers.MyAccount
{
    public partial class MyAccountController : ThemeController
    {
        /// <summary>
        /// 订单列表
        /// </summary>
        /// <param name="orderStatus">订单状态</param>
        /// <param name="pageIndex">当前页的索引</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult OrderList(int orderType = 1, int page = 0, int orderStatus = 0)
        {
            var model = new OrderModel();
            model.FiterIndex = orderType;
            DbQueryResult<List<OrderBasicInfo>> result;
            if (orderStatus > 0)
            {
                result = model.GetMyOrderList(orderType, 0, 1000);
                List<OrderBasicInfo> list = result.Data;
                if (orderStatus == 1)
                {
                    list = result.Data.Where(o => o.OrderStatus == OrderStatus.待支付).ToList();
                }
                else if (orderStatus == 6)
                {
                    list = result.Data.Where(o => o.OrderStatus == OrderStatus.已付款 || o.OrderStatus == OrderStatus.待出库
                        || o.OrderStatus == OrderStatus.已出库 || o.OrderStatus == OrderStatus.已发货).ToList();
                }
                model.OrderList = list.Skip(WebClientContext.CON_PageSize * page).Take(WebClientContext.CON_PageSize).ToList();
                model.OrderTotlaCount = list.Count;
            }
            else
            {
                result = model.GetMyOrderList(orderType, page, WebClientContext.CON_PageSize);
                model.OrderList = result.Data;
                model.OrderTotlaCount = result.TotalRecords;
            }


            ViewBag.Index = page;
            ViewBag.PageSize = WebClientContext.CON_PageSize;
            ViewBag.RecordCount = result.TotalRecords;

            //return View(model);
            //return ManualView("OrderList", model);
            return View(WebClientContext.Current.SiteTheme, "MyAccount", "OrderList", WebClientContext.Current.CacheKeyDefaultMasterPage, model);

        }

        /// <summary>
        /// 订单明细
        /// </summary>
        /// <param name="orderNumber">订单编号</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult OrderDetail(string orderNumber)
        {
            OrderBasicInfoExtend order = OrderModel.GetOrderDetailInfo(orderNumber);

            //var regionList = MyAccountModel.GetRegionListByParentId(0);

            //return View(order);
            //return ManualView("OrderDetail", order);
            return View(WebClientContext.Current.SiteTheme, "MyAccount", "OrderDetail", WebClientContext.Current.CacheKeyDefaultMasterPage, order);

        }

        /// <summary>
        /// 订单明细
        /// </summary>
        /// <param name="orderNumber">订单编号</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult VoidOrder(string orderNumber, int voidType, string voidReason)
        {
            var model = new OrderModel();
            var result = model.VoidOrder(orderNumber, voidType, voidReason);
            return Json(result);
        }

    }
}
